<?php
class Admini_GeneralizeController extends Uuch_ManageController {
    
    public function init() {
        parent::init();
        $this->appendCss('/admins/css/yundan.css');
        $this->appendCss('/admins/css/common.css');
        $this->appendScript('/admins/js/jquery/jquery-1.7.2.min.js')->appendScript('/js/jquery.validate.min.js')->appendScript('/js/messages_cn.js')->appendScript('/js/uuch.js');
        $this->setTitle("推广管理");
        $this->view->type = "general";
    }    
       
    public function indexAction()
    {
        $pagesize = $this->getSafeParam('pagesize');
        if (!$pagesize) {
            $pagesize = 10;
        }
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $this->setTitle('推广管理');
        $quick = $this->getSafeParam('quick');
        $timescope = $this->getSafeParam('timescope');
        $startTime = $this->getSafeParam('startTime');
        $endtTme = $this->getSafeParam('endTime');
        $cityId = $this->getSafeParam("cityId");
        $status = $this->getSafeParam("status");
        $generalizeSearch = new Application_Model_Filter_GeneralizeSearch();
        $generalizeSearch->quick = $quick;
        $generalizeSearch->timescope = $timescope;
        $generalizeSearch->startTime = $startTime;
        $generalizeSearch->endTime = $endtTme;
        $generalizeSearch->cityId = $cityId;
        $generalizeSearch->status = $status;
        $generalizeFilter = Application_Model_Generalize::getFilter($generalizeSearch,$pagesize,$pageindex);
        $this->view->generalizeList = $generalizeFilter["list"];
        $this->view->generalizeAll = $generalizeFilter;
        $this->view->typeAll = Application_Model_GeneralizeRule::getGeneralizeTypeAll();
        $this->view->statusAll = Application_Model_Generalize::getStatusAll();
        $this->view->quick = $quick;
        $this->view->timescope = $timescope;
        $this->view->startTime = $startTime;
        $this->view->endTime = $endtTme;
        $this->view->cityId = $cityId;
        $this->view->status = $status;
        $this->view->pagesize = $pagesize;
        $words = array();
        for ($i = 0; $i < 26; $i++) {
            $words[] = chr(65 + $i);
        }
        $this->view->words = $words;
        $this->view->stype = 1;
    }


    public function addgeneralizdAction()
    {
        if($this->isPost()){
//            var_dump($this->_getAllParams());exit;
            $no = $this->getSafeParam("no");
            if($no == ""){
                $this->message("活动标识不能为空！");
                return;
            }
            $name = $this->getSafeParam("name");
            if($name == ""){
                $this->message("活动名称不能为空！");
                return;
            }
            $content = $this->getSafeParam("content");
            if($content == ""){
                $this->message("活动内容不能为空！");
                return;
            }
            $startTime = $this->getSafeParam("startTime");
            if($startTime == ""){
                $this->message("活动开始不能为空！");
                return;
            }
            $endTime = $this->getSafeParam("endTime");
            if($endTime == ""){
                $this->message("活动结束不能为空！");
                return;
            }
            $cityId = $this->getSafeParam("cityId");
            $status = $this->getSafeParam("status");
            
            $grantType = $this->_getParam("grantType");
            $giftType = $this->_getParam("giftType");
            $giftId = $this->_getParam("giftId");
            $giftNum = $this->_getParam("giftNum");
            $ruleLimit = $this->_getParam("ruleLimit");
            $ruleNum = $this->_getParam("ruleNum");
            $minRule = $this->_getParam("minRule");
            $maxRule = $this->_getParam("maxRule");
            $generalizeType = $this->_getParam("generalizeType");
            $receiveType = $this->_getParam("receiveType");
            $ifGift = true;
            for($i=0;$i<count($grantType);$i++){
                if(($giftNum[$i] > 0 || $giftNum[$i] != "") && $minRule[$i] != "" && $maxRule[$i] != "" && ($ruleNum[$i] > 0 || $ruleNum[$i] != "")) {
                    $ifGift = false;
                    break;
                }
            }
            if($ifGift) {
                $this->message("活动礼物不能为空！");
                return;
            }

            $generalizeId = Application_Model_Generalize::addGeneralize($no, $name, $content, $startTime, $endTime, $cityId, $status);
            Application_Model_Operate::addOperate(0, null, $generalizeId, "添加新活动：".$name, 0);
            Application_Model_GeneralizeRule::addRule($generalizeId,$grantType,$giftType,$giftId,$giftNum,$ruleLimit,$ruleNum,$minRule,$maxRule,$generalizeType,$receiveType);
            $this->view->ma = 1;
        }
        $this->view->generalizeTypeAll = Application_Model_GeneralizeRule::getGeneralizeTypeAll();
        $this->view->statusAll = Application_Model_Generalize::getStatusAll();
        $this->view->giftTypeAll = Application_Model_GeneralizeRule::getGiftTypeAll();
        $this->view->giftAll = Application_Model_CouponBatch::batchall();
        $this->view->grantAll = Application_Model_GeneralizeRule::getGrantAll();
        $this->view->ruleLimitAll = Application_Model_GeneralizeRule::getRuleLimitAll();
        $this->view->receiveTypeAll = Application_Model_GeneralizeRule::getReceiveTypeAll();
        $words = array();
        for ($i = 0; $i < 26; $i++) {
            $words[] = chr(65 + $i);
        }
        $this->view->words = $words;
        $this->view->stype = 1;
    }
    
    public function selgiftAction() {
        $type = $this->getSafeParam("type");
        $giftItem = Application_Model_GeneralizeRule::getGiftList($type);
        $this->json(array("type"=>$giftItem?1:0,"giftItem"=>$giftItem));
    }
    
    public function generalizedetailAction() {
        $this->setTitle("推广活动详情");
        $this->appendCss('/admins/css/adminshare.css');
        $this->appendCss('/admins/css/cangku.css');
        $generalizeId = $this->getSafeParam("id");
        $generalizeItem = Application_Model_Generalize::load($generalizeId);
        if($this->isPost()){
            $arrReceive = $this->_getParam("arrReceive");
            if($arrReceive == ""){
                $no = $this->getSafeParam("no");
                if($no == ""){
                    $this->message("活动标识不能为空！");
                    return;
                }
                $name = $this->getSafeParam("name");
                if($name == ""){
                    $this->message("活动名称不能为空！");
                    return;
                }
                $content = $this->getSafeParam("content");
                if($content == ""){
                    $this->message("活动内容不能为空！");
                    return;
                }
                $startTime = $this->getSafeParam("startTime");
                if($startTime == ""){
                    $this->message("活动开始不能为空！");
                    return;
                }
                $endTime = $this->getSafeParam("endTime");
                if($endTime == ""){
                    $this->message("活动结束不能为空！");
                    return;
                }
                $cityId = $this->getSafeParam("cityId");
                $status = $this->getSafeParam("status");
                $ruleId = $this->_getParam("ruleId");
                $grantType = $this->_getParam("grantType");
                $giftType = $this->_getParam("giftType");
                $giftId = $this->_getParam("giftId");
                $giftNum = $this->_getParam("giftNum");
                $ruleLimit = $this->_getParam("ruleLimit");
                $ruleNum = $this->_getParam("ruleNum");
                $minRule = $this->_getParam("minRule");
                $maxRule = $this->_getParam("maxRule");
                $generalizeType = $this->_getParam("generalizeType");
                $receiveType = $this->_getParam("receiveType");
                $ifGift = true;
                for($i=0;$i<count($grantType);$i++){
                    if(($giftNum[$i] > 0 || $giftNum[$i] != "") && $minRule[$i] != "" && $maxRule[$i] != "" && ($ruleNum[$i] > 0 || $ruleNum[$i] != "")) {
                        $ifGift = false;
                        break;
                    }
                }
                if($ifGift) {
                    $this->message("活动礼物不能为空！");
                    return;
                }
               
                $generalizeItem->editGeneralize($no, $name, $content, $startTime, $endTime, $cityId, $status);
                $generalizeItem->editRule($ruleId,$grantType,$giftType,$giftId,$giftNum,$ruleLimit,$ruleNum,$minRule,$maxRule,$generalizeType,$receiveType);
                //Application_Model_Operate::addOperate(0, null, $generalizeItem->Id, "修改新活动：".$name, 0);
                $this->view->ma = 1;    
            } else {
                $this->view->arrReceive = $arrReceive;
                $this->view->ma = 2; 
            }
        }
        $this->view->generalizeItem = $generalizeItem;
        $this->view->generalizeTypeAll = Application_Model_GeneralizeRule::getGeneralizeTypeAll();
        $this->view->statusAll = Application_Model_Generalize::getStatusAll();
        $this->view->giftTypeAll = Application_Model_GeneralizeRule::getGiftTypeAll();
        $this->view->giftAll = Application_Model_CouponBatch::batchall();
        $this->view->grantAll = Application_Model_GeneralizeRule::getGrantAll();
        $this->view->ruleLimitAll = Application_Model_GeneralizeRule::getRuleLimitAll();
        $this->view->receiveTypeAll = Application_Model_GeneralizeRule::getReceiveTypeAll();
        $words = array();
        for ($i = 0; $i < 26; $i++) {
            $words[] = chr(65 + $i);
        }
        $this->view->words = $words;
        $this->view->stype = 1;
    }
    
    public function enableordisableglAction() {
        $generalizeId = $this->getSafeParam("id");
        $generalizeItem = Application_Model_Generalize::load($generalizeId);
        if($generalizeItem) {
            if($generalizeItem->status == Application_Model_Generalize::Status_Launch) {
                $generalizeItem->status = Application_Model_Generalize::Status_Default;
            } else {
                $generalizeItem->status = Application_Model_Generalize::Status_Launch;
            }
            $generalizeItem->save();
            $this->json(1);
            return ;
        }
        $this->json(0);
    }
    
    public function receiveAction() {
        $id = $this->getSafeParam("id");
        $receiveId = $this->_getParam("receiveId");
        $generalizeItem = Application_Model_Generalize::load($id);
        $arrReceive = array();
        foreach ($receiveId as $receiveKey => $receiveVal) {
            if($receiveVal == ""){
                continue;
            }
            $ruleItem = Application_Model_GeneralizeRule::load($receiveKey);
            $reItem = Application_Model_Generalize::createdReceive($ruleItem->grantType,$receiveVal);
            if(!$reItem) {
                $this->message("参与活动的Id（".$receiveVal."）不存在，请重新输入", "返回", "/admini/generalize/generalizedetail/id/".$id);
                return ;
            }
            $userId = Application_Model_Generalize::getReceiveUserId($ruleItem->grantType, $reItem);
            $result = $generalizeItem->receive($userId,$ruleItem->grantType,$receiveVal);
            $arrReceive[$receiveVal] = $result;
        }
        $this->_forward("generalizedetail", "generalize", "admini", array("arrReceive"=>$arrReceive));
    }
    
    public function receivedetailAction() {
        $id = $this->getSafeParam("gelId");
        $quick = $this->getSafeParam("quick");
        $grantType = $this->getSafeParam("grantType");
        $giftType = $this->getSafeParam("giftType");
        $giftId = $this->getSafeParam("giftId");
        $pagesize = $this->getSafeParam('pagesize');
        $starttime = $this->getSafeParam("starttime");
        $endtime = $this->getSafeParam("endtime");
        if (!$pagesize) {
            $pagesize = 10;
        }
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $receiveItem = Application_Model_GeneralizeReceive::getFilter($id,$quick,$starttime,$endtime,$grantType,$giftType,$giftId,$pagesize,$pageindex);
        $this->view->receiveList = $receiveItem["list"];
        $this->view->receiveAll = $receiveItem;
        $this->view->gelId = $id;
        $this->view->quick = $quick;
        $this->view->grantType = $grantType;
        $this->view->giftType = $giftType;
        $this->view->giftId = $giftId;
        $this->view->pagesize = $pagesize;
        $this->view->starttime = $starttime;
        $this->view->endtime = $endtime;
        $this->view->giftAll = Application_Model_GeneralizeRule::getGiftList($giftType);
        $this->view->giftTypeAll = Application_Model_GeneralizeRule::getGiftTypeAll();
        $this->view->grantAll = Application_Model_GeneralizeRule::getGrantAll();
        $this->view->generalizeAll = Application_Model_Generalize::getAll();
        $this->view->generalizeItem = Application_Model_Generalize::load($id);
        $this->view->stype = 1;
    }
}
